package metro.tourniquet;


import metro.system.IdGenerator;
import metro.response.Response;
import metro.system.MetroSystem;

public class Tourniquet {
    public static final int OPEN=1,CLOSE=2;
    private static final String DEFAULT_MESSAGE="Put card on sensor";
    private int status;
    private long id;
    private String message;
    private MetroSystem metroSystem;

    public Tourniquet() {
        id = IdGenerator.generateId();
        status=CLOSE;
    }

    public Tourniquet(MetroSystem metroSystem) {
        this();
        setMetroSystem(metroSystem);
    }

    public void pass(long cardId){
        Response response= metroSystem.pass(this,cardId);
        if (response.isSuccess()){
            open(response.getMessage());
        } else {
            showMessage(response.getMessage());
        }
    }


    private void open(String message){
        status=OPEN;
        showMessage(message);
    }

    private void close(){
        status=CLOSE;
        showMessage(DEFAULT_MESSAGE);
    }

    private void showMessage(String message){
        this.message=message;
    }

    public void setMetroSystem(MetroSystem metroSystem) {
        this.metroSystem = metroSystem;
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public long getId() {
        return id;
    }
}
